<!-- 维修详情 》 工单底部 -->

<template>
  <div class="DetailDevice-RepairFooter">
    <BtnPrimary v-if="config.hasRelease" title="发布" @click="openRelease(one)" />
    <BtnDanger v-if="config.hasRelease" title="删除" red @click="openDelete(one)" />

    <BtnPrimary v-if="config.btn" :title="config.btn" @click="openDeal(one)" />
    <BtnPrimary v-else-if="hasRate(one)" title="评价" @click="openDeal(one)" />

    <BtnPrimary v-if="config.btn2" :title="config.btn2" @click="openDeal(one, 'deal2')" />

    <BtnDanger v-if="!config.noCancel" title="撤销" @click="openCancel(one)" />
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'RepairFooter',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb(), CORE.mixWeb('deal', 'deal_')],

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 状态配置
     */
    config() {
      const { MAP, one } = this
      return MAP[one.status] || { noCancel: true }
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 打开发布弹窗
     */
    async openRelease(one) {
      await CORE.CONFIRM.success('确定发布此条工单吗？', '发布工单')

      this.setOne(one)
      this.startEdit()
    },

    /**
     * 打开删除弹窗
     */
    async openDelete(one) {
      await CORE.CONFIRM.error('确定删除此条工单吗？', '删除工单')
      this.setOne(one)
      this.startDel()
    },

    /**
     * 打开处理弹窗
     */
    openDeal(one, dealKey = 'deal') {
      const { MAP, $root: { repair_assign_selectDict: assign } } = this
      const DEAL = MAP[one.status][dealKey]

      const obj = { workOrderId: one.id, ...MAP[DEAL] }

      if (['ASSIGN', 'FORWARD'].includes(DEAL)) {
        const [, organId, uaaUserId, name, phone] = assign.find(item => item[0] === one.orderSubTypeId) || []
        CORE.merge(obj, {
          assignUserId: uaaUserId,
          assignUserName: name,
          assignUserPhone: phone,
          user: { uaaUserId, name, phone, organId: [organId] }, // 指派人
        })
      }

      this.deal_MAP.tipEdit = obj.operate

      this.deal_openEdit(obj)
    },

    /**
     * 打开撤销弹窗
     */
    openCancel(one) {
      const { MAP } = this
      const obj = { workOrderId: one.id, ...MAP.CANCEL }

      this.deal_MAP.tipEdit = obj.operate

      this.deal_openEdit(obj)
    },

    /**
     * 有无评价
     */
    hasRate(one) {
      return one.status === 'task_complete' && one.orderSourceCode === 'task_pc' && one.evaluateTask !== 1
    },
  },
}
</script>
